package com.ruoyi.mp.controller;

import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.mp.service.IMpAccountService;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.invoice.reimburse.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <h3>springcloudSaas</h3>
 * <p>电子发票报销方相关接口</p>
 *
 * @author : 郎兴宇
 * @date : 2024-10-07 19:13
 **/
@RestController
@RequestMapping("/reimburse")
public class MpReimburseInvoiceController extends BaseController {
    @Autowired
    private IMpAccountService iMpAccountService;
    @Autowired
    private WxMpService wxService;

    /**
     * 批量查询报销发票信息
     */
    @RequiresPermissions("mp:reimburse:getInvoiceBatch")
    @Log(title = "批量查询报销发票信息.", businessType = BusinessType.EXPORT)
    @PostMapping("/getInvoiceBatch/{id}")
    public AjaxResult getInvoiceBatch(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        InvoiceBatchRequest request  = new InvoiceBatchRequest();
        List<InvoiceInfoResponse> invoiceInfoResponses =  wxService.switchoverTo(appId).getReimburseInvoiceService().getInvoiceBatch(request);
        return AjaxResult.success(invoiceInfoResponses);
    }


    /**
     * 查询报销发票信息
     */
    @RequiresPermissions("mp:reimburse:getInvoiceInfo")
    @Log(title = "查询报销发票信息.", businessType = BusinessType.EXPORT)
    @PostMapping("/getInvoiceInfo/{id}")
    public AjaxResult getInvoiceInfo(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        InvoiceInfoRequest request  = new InvoiceInfoRequest();
        InvoiceInfoResponse invoiceInfoResponses =  wxService.switchoverTo(appId).getReimburseInvoiceService().getInvoiceInfo(request);
        return AjaxResult.success(invoiceInfoResponses);
    }


    /**
     * 更新发票状态
     */
    @RequiresPermissions("mp:reimburse:updateInvoiceStatus")
    @Log(title = "更新发票状态.", businessType = BusinessType.EXPORT)
    @PutMapping("/updateInvoiceStatus/{id}")
    public AjaxResult updateInvoiceStatus(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        UpdateInvoiceStatusRequest updateInvoiceStatusRequest  = new UpdateInvoiceStatusRequest();
          wxService.switchoverTo(appId).getReimburseInvoiceService().	updateInvoiceStatus(updateInvoiceStatusRequest);
        return AjaxResult.success();
    }


    /**
     * 批量更新发票状态
     */
    @RequiresPermissions("mp:reimburse:updateStatusBatch")
    @Log(title = "批量更新发票状态.", businessType = BusinessType.EXPORT)
    @PutMapping("/updateStatusBatch/{id}")
    public AjaxResult updateStatusBatch(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        UpdateStatusBatchRequest updateStatusBatchRequest = new UpdateStatusBatchRequest();
        wxService.switchoverTo(appId).getReimburseInvoiceService().	updateStatusBatch(updateStatusBatchRequest);
        return AjaxResult.success();
    }
}


